package bustime;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.andro.chansung.ConstantName;
import com.andro.chansung.ConstantURL;
import com.andro.chansung.DB;
import com.andro.chansung.R;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class BusDetailFragment extends Fragment implements OnClickListener, OnItemClickListener {

	private String name;
	private BusDetailHandler handler;
	private GridView comingGridView;
	private GridView leavingGridView;
	private TextView titleTv;
	private ProgressDialog pd;
	
	public BusDetailFragment(String name) {
		this.name = name;
	}
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		SQLiteDatabase db = getActivity().openOrCreateDatabase(ConstantName.DATABASE_NAME, Context.MODE_WORLD_READABLE, null);
		DB.removeBusDetailTables(db);
		DB.createBusDetailTables(db);
		db.close();
		
		View view = inflater.inflate(R.layout.bus_detail_show_layout, container, false);
		
		titleTv = (TextView)view.findViewById(R.id.bus_detail_destination_title_tv);
		titleTv.setText(name + "[학기중(주중)]");
		
		comingGridView = (GridView)view.findViewById(R.id.bus_detail_gridview);
		leavingGridView = (GridView)view.findViewById(R.id.bus_detail_destination_leaving_gridView);
		
		comingGridView.setOnItemClickListener(this);
		leavingGridView.setOnItemClickListener(this);
		
		Button semesterWeekBtn = (Button)view.findViewById(R.id.bus_detail_semester_week_btn);
		Button semesterWeekendBtn = (Button)view.findViewById(R.id.bus_detail_semester_weekend_btn);
		Button vacationWeekBtn = (Button)view.findViewById(R.id.bus_detail_vacation_week_btn);
		Button vacationWeekendBtn = (Button)view.findViewById(R.id.bus_detail_vacation_weekend_btn);
		
		semesterWeekBtn.setOnClickListener(this);
		semesterWeekendBtn.setOnClickListener(this);
		vacationWeekBtn.setOnClickListener(this);
		vacationWeekendBtn.setOnClickListener(this);
		
		handler = new BusDetailHandler();
		Calendar calendar = Calendar.getInstance();
		BusDetailUpdateThread thread = new BusDetailUpdateThread(getActivity(), handler, ConstantURL.BUS_WEEK_SEMESTER_TIME_URL, name, calendar);
		pd = ProgressDialog.show(getActivity(), "로딩중", "버스 정보 로딩중..");
		thread.start();
		
		return view;
	}
	
	private class BusDetailHandler extends Handler {
		public void handleMessage(Message msg) {
			SQLiteDatabase db = getActivity().openOrCreateDatabase(ConstantName.DATABASE_NAME, Context.MODE_WORLD_READABLE, null);
			List<BusDetail> busList = DB.getDetailBusDataFromBusTableByName(db, name);
			db.close();
			if( busList.isEmpty() ) {
				Toast.makeText(getActivity(), "버스가 제공되지 않습니다", Toast.LENGTH_SHORT).show();
			} else {
				
				List<BusDetail> comingList = new ArrayList<BusDetail>();
				List<BusDetail> leavingList = new ArrayList<BusDetail>();
				
				for( BusDetail bus : busList ) {
					if( bus.isComeOrLeave() ) {
						comingList.add(bus);
					} else {
						leavingList.add(bus);
					}
				}
				
				comingGridView.setAdapter(new BusDetailAdapter(getActivity(), comingList));
				leavingGridView.setAdapter(new BusDetailAdapter(getActivity(), leavingList));
			}
			pd.dismiss();
		}
	}

	public void onClick(View v) {
		SQLiteDatabase db = getActivity().openOrCreateDatabase(ConstantName.DATABASE_NAME, Context.MODE_WORLD_READABLE, null);
		DB.removeBusDetailTables(db);
		DB.createBusDetailTables(db);
		db.close();
		
		BusDetailUpdateThread thread = null;
		switch( v.getId() ) {
		case R.id.bus_detail_semester_week_btn :
			thread = new BusDetailUpdateThread(getActivity(), handler, ConstantURL.BUS_WEEK_SEMESTER_TIME_URL, name, null);
			titleTv.setText(name + "[학기중(주중)]");
			break;
			
		case R.id.bus_detail_semester_weekend_btn :
			thread = new BusDetailUpdateThread(getActivity(), handler, ConstantURL.BUS_WEEKEND_SEMESTER_TIME_URL, name, null);
			titleTv.setText(name + "[학기중(토요일)]");
			break;
			
		case R.id.bus_detail_vacation_week_btn :
			thread = new BusDetailUpdateThread(getActivity(), handler, ConstantURL.BUS_WEEK_VACATION_TIME_URL, name, null);
			titleTv.setText(name + "[방학중(주중))]");
			break;
			
		case R.id.bus_detail_vacation_weekend_btn :
			thread = new BusDetailUpdateThread(getActivity(), handler, ConstantURL.BUS_WEEKEND_VACATION_TIME_URL,name, null);
			titleTv.setText(name + "[방학중(토요일)]");
			break;
		}
		pd = ProgressDialog.show(getActivity(), "로딩중", "버스 정보 로딩중..");
		thread.start();
	}

	public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
		SQLiteDatabase db = getActivity().openOrCreateDatabase(ConstantName.DATABASE_NAME, Context.MODE_WORLD_READABLE, null);
		List<BusDetail> busList = DB.getDetailBusDataFromBusTableByName(db, name);
		db.close();		
		
		List<BusDetail> comingList = new ArrayList<BusDetail>();
		List<BusDetail> leavingList = new ArrayList<BusDetail>();
		
		for( BusDetail bus : busList ) {
			if( bus.isComeOrLeave() ) {
				comingList.add(bus);
			} else {
				leavingList.add(bus);
			}
		}
		
		AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
		alert.setTitle("부가 정보");
		alert.setPositiveButton("OK", null);
		switch( parent.getId() ) {
		case R.id.bus_detail_gridview :
			alert.setMessage(comingList.get(position).getDescription());
			break;
			
		case R.id.bus_detail_destination_leaving_gridView :
			alert.setMessage(leavingList.get(position).getDescription());
			break;
		}
		alert.show();
	}
}
